﻿using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Xml;
using System.IO;
using System.Collections.Specialized;

namespace Util
{
    public static class Common
    {
        public static string GetSystemLanguage
        {
            get
            {
                return System.Globalization.CultureInfo.CurrentCulture.IetfLanguageTag;
            }
        }
        public static string GetXmlSingleNodeValue(string XmlFile, string XmlPath)
        {
            try
            {
                XmlDocument XDoc = new XmlDocument();
                XDoc.Load(XmlFile);
                return XDoc.SelectSingleNode(XmlPath).InnerText;
            }
            catch
            {
                return String.Empty;
            }
        }

        //public static void OpenErrorPage(string message, MasterPage master)
        //{
        //    userControls_ucHataPopUp popup = (DevExpress.Web.ASPxPopupControl.ASPxPopupControl)FindControl(master, "");
        //}

        public static string GetCurrentPageName()
        {
            string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
            System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
            string sRet = oInfo.Name;
            return sRet;
        }

        public static string GetCurrentPageFilePath()
        {
            return System.Web.HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath;
        }

        public static decimal FixDecimal(string strValue)
        {
            decimal result;
            Decimal.TryParse(strValue, out result);
            return result;
        }

        public static void ClearControl(Control control)
        {
            
            RadioButtonList rbl;
            TextBox txt;
            RadioButton rb;
            DropDownList ddl;

            for (int i = 0; i < control.Controls.Count; i++)
            {
                if (control.Controls[i].GetType() == typeof(RadioButtonList))
                {
                    rbl = (RadioButtonList)control.FindControl(control.Controls[i].ID);
                    rbl.SelectedIndex = 0;
                }
                else if (control.Controls[i].GetType() == typeof(RadioButton))
                {
                    rb = (RadioButton)control.FindControl(control.Controls[i].ID);
                    rb.Checked = false;
                }
                else if (control.Controls[i].GetType() == typeof(TextBox))
                {
                    txt = (TextBox)control.FindControl(control.Controls[i].ID);
                    txt.Text = String.Empty;
                }
                else if (control.Controls[i].GetType() == typeof(DropDownList))
                {
                    ddl = (DropDownList)control.FindControl(control.Controls[i].ID);
                    ddl.SelectedIndex = 0;
                }
            }
        }

        public static System.Web.UI.Control FindControl(System.Web.UI.Control mainControl, string findControl)
        {
            if (mainControl == null)
                return null;

            System.Web.UI.Control control;
            control = mainControl.FindControl(findControl);
            if (control != null)
                return control;
            foreach (Control control2 in mainControl.Controls)
            {
                control = FindControl(control2, findControl);
                if (control != null)
                    return control;
            }
            return null;
        }

        public static string ClearHTML(string data)
        {
            string pattern = @"<(.|\n)*?>";
            return Regex.Replace(data, pattern, string.Empty);
        }

        public static string HashPassword(string data)
        {
            data = FormsAuthentication.HashPasswordForStoringInConfigFile(data, "md5");
            data = FormsAuthentication.HashPasswordForStoringInConfigFile(data, "sha1");
            data = FormsAuthentication.HashPasswordForStoringInConfigFile(data, "md5");
            data = FormsAuthentication.HashPasswordForStoringInConfigFile(data, "sha1");
            
            return data;
        }

        public static string[] doParse(string data, char parseSymbol)
        {
            string[] parsed = data.Split(new Char[] { parseSymbol });
            if (parsed[parsed.Length - 1] == String.Empty)
                Array.Resize(ref parsed, parsed.Length - 1);
            return parsed;
        }

        public static string GeneratePaswword(int size)
        {
            size = (size < 3) ? 3 : size; //şifre boyutu en az 3 karakter olmalıdır.
            double[] sayi = new double[4];
            //harf sayısı
            sayi[0] = ((size * 0.5) < 1) ? 1 : Math.Floor(size * 0.5); //şifrenin yarısı harf olacak
            //rakam sayısı
            sayi[1] = ((size * 0.3) < 1) ? 1 : Math.Floor(size * 0.3); //şifrenin %30 u rakam olacak
            //karakter sayısı
            sayi[2] = ((size * 0.2) < 1) ? 1 : Math.Floor(size * 0.2); //şifrenin %20 si karakter olacak
            //kalan
            sayi[3] = (sayi[0] + sayi[1] + sayi[2]) == size ? 0 : size - (sayi[0] + sayi[1] + sayi[2]);

            string sifre = string.Empty;
            string[] karakter = { ".", ",", " ", "*", "=", ")", "(", "/", "&", "%", "+", "^", "'", "!", "\"", ">", "£", "#", "$", "½", "{", "[", "]", "}", "\\", "|", "-", "<", "~", ";", "_" };
            string[] harf = { "a", "b", "c", "d", "e", "f", "g", "h", "ı", "i", "j", "k", "l", "m", "n", "o", "ö", "p", "r", "s", "ş", "t", "u", "ü", "w", "x", "q", "v", "y", "z" };
            Random rnd = new Random();
            double boyutTemp = 0;
            for (int i = 0; i < sayi.Length; i++)
            {
                boyutTemp += sayi[i];
                for (int j = 0; j < sayi[i]; j++)
                {
                    switch (i)
                    {
                        case 0: { sifre += harf[rnd.Next(0, harf.Length)]; break; }
                        case 1: { sifre = sifre.Insert(rnd.Next(1, sifre.Length), (rnd.Next(0, 9)).ToString()); break; }
                        case 2: { sifre = sifre.Insert(rnd.Next(1, sifre.Length), karakter[rnd.Next(0, karakter.Length)]); break; }
                        case 3: { sifre += harf[rnd.Next(0, harf.Length)]; break; }
                    }
                }
            }
            return sifre;
        }

        public static string OnlyDate(object tarih)
        {//tarih verisinden saati ayıklar
            if (tarih != null && tarih.ToString() != string.Empty)
                return Convert.ToDateTime(tarih).ToShortDateString();
            return string.Empty;
        }

        public static string CutString(object metin)
        {//metni istenilen karakter boyutlarına getirir.
            int karakterSayisi = 200;
            return (metin.ToString().Length < karakterSayisi) ? metin.ToString() : metin.ToString().Substring(0, karakterSayisi) + "...";
        }
    }
}
